﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class OpeThing_vindicate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string sql = "select * from OcThingsType ";
            SqlCommand com = new SqlCommand(sql,con);
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read())
            {
                DropDownList1.Items.Add(new ListItem(dr["name"].ToString(),dr["sid"].ToString()));
            }
            con.Close();
        }
    }
    protected void bnSave_ServerClick(object sender, EventArgs e)
    {
        if (this.txName.Value.Trim() != null)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            int Typesid = Convert.ToInt32(DropDownList1.SelectedItem.Value.ToString());
            string sql = "insert OcThings values('"+txName.Value.Trim()+"',"+Typesid+")";
            SqlCommand com = new SqlCommand(sql,con);
            com.ExecuteNonQuery();
            con.Close();
            Insert();
            Response.Write("<script>alert('操作成功');window.location.href='Info.aspx';</script>");
        }
    }
    public void Insert()
    {
        string Worktype="采购";
        int Count = Convert.ToInt32(this.Text1.Value.Trim());
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string Name=this.txName.Value.Trim();
       int sids= SelectSid(Name);
        int sid=Convert.ToInt32(DropDownList1.SelectedItem.Value); 
        string Content=this.txContent.Value.Trim();
        string sql = "insert into OcThing values('" + Worktype + "'," + Count + ",'" + sid + "','" + DropDownList1.SelectedItem.Text + "','" + sid + "','" + Name + "','" + Content + "','" + Session["UserName"].ToString() + "')";
        SqlCommand com = new SqlCommand(sql,con);
        com.ExecuteNonQuery();
        con.Close();
    }
    public int SelectSid(string Name)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select sid from  OcThings where name='"+Name+"'";
        SqlCommand com = new SqlCommand(sql,con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        int i = 0;
        i=Convert.ToInt32(dr["sid"].ToString());
        return i;
       
    }
 
}
